import {post, fetch} from 'plugins/axios'

export function openInfo ({state, dispatch, commit}, payload) {
  Promise.all([
    dispatch('loadSelect', payload),
    dispatch('loadInfo', payload)
  ]).then((result) => {
    console.log('done')
  }).catch((error) => {
    console.log(error)
  })
}

export function loadInfo ({state, commit}, payload) {
  state.interval = setInterval(() => {
    let url = '/publisher/restart_publish_info/'
    post(url, {
      opt: payload
    }).then((response) => {
      commit('loadInfoSuccess', response)
    }).catch((error) => {
      alert(error.response.data)
    })
  }, 2000)
}

export function loadSelect ({state, commit}, payload) {
  fetch('/publisher/get_host/', {
    project: payload
  }).then((response) => {
    commit('loadSelectSuccess', response)
  }).catch((error) => {
    console.log(error.response)
  })
}

export function restartService ({state, commit}, payload) {
  post('/publisher/restart_publish/', {
    opt: payload,
    cmd: state.recentCmd,
    hosts: state.fields.host
  }).then((response) => {
    alert(response)
    state.verifyDialog = false
  }).catch((error) => {
    alert(error.response.data)
  })
}
